import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(primarySwatch: Colors.pink),
        home: Scaffold(
          appBar: AppBar(title: Text('StatefulWidget')),
          body: HomePage(),
        ));
  }
}

// 自定义有状态组件
class HomePage extends StatefulWidget {
  //StatefulWidget是个abstract抽象类，必须执行里面的抽象方法createState()
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int count = 1;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            '${this.count}',
            style: TextStyle(fontSize: 30),
          ),
          MyButton(
            '按钮',
            onPriessed: () {
              setState(() {
                this.count++;
              });
            },
          ),
        ],
      ),
    );
  }
}

class MyButton extends StatelessWidget {
  final String text;
  final onPriessed;

  MyButton(this.text, {this.onPriessed});

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text(this.text),
      textColor: Theme.of(context).accentColor,
      onPressed: () {
        this.onPriessed();
      },
    );
  }
}
